﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace BoarAccess
{
    public class FileInfo
    {
        /**        "filename": "Uniarbeit/AG RA/Paper - Data Extraction/main.log", 
        "md5sum": "43912c2fdafc08c03747caae8097f879", 
        "mtime": 1361486122, 
        "ctime": 1360618853, 
        "size": 39487 **/

        public string Filename, MD5;
        public int Size;
        public DateTime Modified, Created;
        public SessionInfo ParentSession;

        public override string ToString()
        {
            return Filename + " in " + ParentSession;
        }

        public static IEnumerable<FileInfo> LoadFromJSON(string filename, SessionInfo parent)
        {
            List<FileInfo> result = new List<FileInfo>();

            JArray files = JArray.Parse(LocalSession.resources.RetrieveFileResource(filename));

            foreach (var item in files)
            {
                if (item["action"] != null)
                    continue;
                FileInfo current = new FileInfo();
                current.Filename = (string)item["filename"];
                current.MD5 = (string)item["md5sum"];
                current.Modified = ResourceManager.FromUnix((long)item["mtime"]);
                current.Created = ResourceManager.FromUnix((long)item["ctime"]);
                current.Size = (int)item["size"];
                current.ParentSession = parent;
                result.Add(current);
            }

            return result;
        }
    }
}
